home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Sample Code Update 01⁄96 / Fragment Tool / Sources / Streams.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-20  |  3.9 KB  |  217 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        Streams.c
  3.  
  4.     Contains:    Utility routines to stream data into a block of memory
  5.  
  6.     Written by:    Chris White, Developer Technical Support
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.     
  10.     Change History (most recent first):
  11.     
  12.                   9/28/95    CW        First release
  13.  
  14. */
  15.  
  16.  
  17. #ifndef __TYPES__
  18.     #include <Types.h>
  19. #endif
  20.  
  21. #ifdef __MEMORY__
  22.     #include <Memory.h>
  23. #endif
  24.  
  25. #ifndef __ERRORS__
  26.     #include <Errors.h>
  27. #endif
  28.  
  29.  
  30. #ifndef __STREAMS__
  31.     #include "Streams.h"
  32. #endif
  33.  
  34.  
  35.  
  36.  
  37.  
  38. OSErr NewStream ( tStreamRef* streamRef, Size blockSize )
  39. {
  40.     OSErr                theErr;
  41.     Ptr                 thePtr;
  42.     tStreamHeaderPtr    theHeader;
  43.     
  44.     
  45.     *streamRef = nil;
  46.     thePtr = NewPtr ( sizeof ( tStreamHeader ) + blockSize );
  47.     theErr = MemError ( );
  48.     if ( theErr )
  49.         return theErr;
  50.     
  51.     theHeader = (tStreamHeaderPtr) thePtr;
  52.     theHeader->blockSize = blockSize;
  53.     theHeader->currentSize = sizeof ( tStreamHeader ) + blockSize;
  54.     theHeader->cursor = sizeof ( tStreamHeader );
  55.     theHeader->maxCursor = sizeof ( tStreamHeader );
  56.     
  57.     *streamRef = thePtr;
  58.     
  59.     return noErr;
  60. }
  61.  
  62.  
  63.  
  64. OSErr DisposeStream ( tStreamRef streamRef )
  65. {
  66.     OSErr    theErr;
  67.     
  68.     DisposePtr ( streamRef );
  69.     theErr = MemError ( );
  70.     if ( theErr == memWZErr )
  71.         return kInvalidStreamRef;
  72.     
  73.     return theErr;
  74. }
  75.  
  76.  
  77.  
  78. OSErr SetStreamData ( tStreamRef streamRef, Ptr dataPtr, Size dataSize )
  79. {
  80.     OSErr                theErr;
  81.     tStreamHeaderPtr    theHeader;
  82.     
  83.     theHeader = (tStreamHeaderPtr) streamRef;
  84.     
  85.     /* Allocate more memory if we need to */
  86.     if ( theHeader->cursor + dataSize > theHeader->currentSize )
  87.     {
  88.         SetPtrSize ( streamRef, theHeader->currentSize + theHeader->blockSize );
  89.         theErr = MemError ( );
  90.         if ( theErr )
  91.             return theErr;
  92.         
  93.         theHeader->currentSize += theHeader->blockSize;
  94.     }
  95.     
  96.     if ( dataPtr )
  97.         BlockMoveData ( dataPtr, streamRef + theHeader->cursor, dataSize );
  98.     theHeader->cursor += dataSize;
  99.     
  100.     if ( theHeader->cursor > theHeader->maxCursor )
  101.         theHeader->maxCursor = theHeader->cursor;
  102.     
  103.     return noErr;
  104. }
  105.  
  106.  
  107.  
  108. OSErr GetStreamData ( tStreamRef streamRef, Ptr dataPtr, Size dataSize )
  109. {
  110.     tStreamHeaderPtr    theHeader;
  111.     
  112.     theHeader = (tStreamHeaderPtr) streamRef;
  113.     if ( theHeader->cursor + dataSize > theHeader->maxCursor )
  114.         return kBoundsErr;
  115.         
  116.     BlockMoveData ( streamRef + theHeader->cursor, dataPtr, dataSize );
  117.     theHeader->cursor += dataSize;
  118.     
  119.     return noErr;
  120. }
  121.  
  122.  
  123.  
  124. OSErr GetStreamDataPtr ( tStreamRef streamRef, Ptr* dataPtr )
  125. {
  126.     OSErr                theErr;
  127.     Ptr                    thePtr;
  128.     Size                theSize;
  129.     tStreamHeaderPtr    theHeader;
  130.     
  131.     *dataPtr = nil;
  132.     theHeader = (tStreamHeaderPtr) streamRef;
  133.     theSize = GetStreamDataSize ( streamRef );
  134.     
  135.     thePtr = NewPtr ( theSize );
  136.     theErr = MemError ( );
  137.     if ( theErr )
  138.         return theErr;
  139.         
  140.     BlockMoveData ( streamRef + sizeof ( tStreamHeader ), thePtr, theSize );
  141.     *dataPtr = thePtr;
  142.     
  143.     return noErr;
  144. }
  145.  
  146.  
  147.  
  148. Size GetStreamDataSize ( tStreamRef streamRef )
  149. {
  150.     tStreamHeaderPtr    theHeader;
  151.     
  152.     theHeader = (tStreamHeaderPtr) streamRef;
  153.     return theHeader->maxCursor - sizeof ( tStreamHeader );
  154. }
  155.  
  156.  
  157.  
  158. tStreamCursor GetStreamCursor ( tStreamRef streamRef )
  159. {
  160.     tStreamHeaderPtr    theHeader;
  161.     
  162.     theHeader = (tStreamHeaderPtr) streamRef;
  163.     return theHeader->cursor;
  164. }
  165.  
  166.  
  167.  
  168. OSErr SetStreamCursor ( tStreamRef streamRef, tStreamCursor cursor )
  169. {
  170.     tStreamHeaderPtr    theHeader;
  171.     
  172.     theHeader = (tStreamHeaderPtr) streamRef;
  173.     if ( cursor > theHeader->currentSize )
  174.         return kBoundsErr;
  175.     if ( cursor < sizeof ( tStreamHeader ) )
  176.         return kBoundsErr;
  177.         
  178.     theHeader->cursor = cursor;
  179.     
  180.     return noErr;
  181. }
  182.  
  183.  
  184.  
  185. void ResetStreamCursor ( tStreamRef streamRef )
  186. {
  187.     tStreamHeaderPtr    theHeader;
  188.     
  189.     theHeader = (tStreamHeaderPtr) streamRef;
  190.     theHeader->cursor = sizeof ( tStreamHeader );
  191.     
  192.     return;
  193. }
  194.  
  195.  
  196.  
  197. OSErr CompactStream ( tStreamRef streamRef )
  198. {
  199.     OSErr                theErr;
  200.     tStreamHeaderPtr    theHeader;
  201.     
  202.     theHeader = (tStreamHeaderPtr) streamRef;
  203.     SetPtrSize ( streamRef, theHeader->maxCursor );
  204.     theErr = MemError ( );
  205.     if ( theErr == memWZErr )
  206.         return kInvalidStreamRef;
  207.     
  208.     theHeader->currentSize = theHeader->maxCursor;
  209.     
  210.     return theErr;
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.